import numpy as np
from sklearn.manifold import TSNE
import umap 

# 生成高维向量数据
def generate_high_dim_vectors(num_points=1000, dimensions=50):
    """生成随机高维向量"""
    np.random.seed(42)
    data = np.random.rand(num_points, dimensions)
    return data

# 生成高维向量
high_dim_vectors = generate_high_dim_vectors(num_points=1000, dimensions=50)

# 使用 t-SNE 进行降维
def apply_tsne(data, n_components=2, perplexity=30, n_iter=1000):
    """使用 t-SNE 进行降维"""
    tsne = TSNE(n_components=n_components, perplexity=perplexity,
                n_iter=n_iter, random_state=42)
    reduced_data = tsne.fit_transform(data)
    return reduced_data

tsne_result = apply_tsne(high_dim_vectors)
print("t-SNE 降维后数据形状:", tsne_result.shape)

# 使用 UMAP 进行降维
def apply_umap(data, n_components=2, n_neighbors=15, min_dist=0.1):
    """使用 UMAP 进行降维"""
    reducer = umap.UMAP(n_components=n_components,
                        n_neighbors=n_neighbors, min_dist=min_dist, random_state=42)
    reduced_data = reducer.fit_transform(data)
    return reduced_data

umap_result = apply_umap(high_dim_vectors)
print("UMAP 降维后数据形状:", umap_result.shape)

# 对比降维结果
# 计算降维后数据的均值和标准差
tsne_mean = np.mean(tsne_result, axis=0)
tsne_std = np.std(tsne_result, axis=0)
umap_mean = np.mean(umap_result, axis=0)
umap_std = np.std(umap_result, axis=0)

print("\nt-SNE 降维后数据的均值:", tsne_mean)
print("t-SNE 降维后数据的标准差:", tsne_std)
print("\nUMAP 降维后数据的均值:", umap_mean)
print("UMAP 降维后数据的标准差:", umap_std)